<template>
  <div class="home" id="home">
    <!-- 搜索 -->
    <van-search
      v-model="searchValue"
      input-align="center"
      placeholder="请输入搜索关键词"
    />
    <!-- 轮播图 -->
    <van-swipe :autoplay="2000" :width="375" :height="200">
      <van-swipe-item v-for="(image, index) in images" :key="index">
        <img width="100%" height="100%" v-lazy="image.image_url" />
      </van-swipe-item>
    </van-swipe>
    <!-- 选择模块 -->
    <van-grid :column-num="5">
      <van-grid-item
        v-for="(item, index) in channel"
        icon="photo-o"
        :text="item.name"
        :key="index"
        :icon="item.icon_url"
      />
    </van-grid>
    <!-- 品牌制造商直供 -->
    <div class="brandList">
      <van-panel title="品牌制造商直供">
        <van-grid :column-num="2">
          <van-grid-item v-for="(item, index) in brandList" :key="index">
            <van-image :src="item.new_pic_url" fit="cover" lazy-load />
            <h4 class="title">{{ item.name }}</h4>
            <p class="price">{{ item.floor_price }}元起</p>
          </van-grid-item>
        </van-grid>
      </van-panel>
    </div>

    <div class="newGoodsList">
      <van-panel title="品牌制造商直供">
        <van-grid :column-num="2">
          <van-grid-item v-for="(item, index) in newGoodsList" :key="index">
            <van-image :src="item.list_pic_url" fit="cover" lazy-load />
            <h5 class="title">{{ item.name }}</h5>
            <p class="price">{{ item.retail_price }}元起</p>
          </van-grid-item>
        </van-grid>
      </van-panel>
    </div>

    
    <!-- 人气榜 -->
    <div class="popularity">
      <van-panel title="人气榜">
        <van-card v-for="(item,index) in hotGoodsList"
          :key="index"
           :price="item.retail_price"
           :desc="item.goods_brief"
           :title="item.name"
           :thumb="item.list_pic_url"
        />
      </van-panel>
      
      <tab-bar></tab-bar>
    </div>
  </div>
  
</template>

<script>
// @ is an alias to /src
import axios from "axios";
import api from "../assets/config/api";
import Vue from "vue";
import TabBar from '@/components/TabBar'
import { Lazyload, Grid, GridItem } from "vant";
Vue.use(Lazyload, Grid, GridItem);

export default {
  name: "Home",
  components: {
    TabBar
  },
  data() {
    return {
      searchValue: "",
      data: {},
    };
  },
  computed: {
    images() {
      if (typeof this.data.banner == "object") {
        return this.data.banner;
      } else {
        return [];
      }
    },
    channel() {
      if (typeof this.data.channel == "object") {
        return this.data.channel;
      } else {
        return [];
      }
    },
    brandList() {
      if (typeof this.data.brandList == "object") {
        return this.data.brandList;
      } else {
        return [];
      }
    },
    newGoodsList() {
      if (typeof this.data.newGoodsList == "object") {
        return this.data.newGoodsList;
      } else {
        return [];
      }
    },
    hotGoodsList() {
      if (typeof this.data.hotGoodsList == 'object') {
        return this.data.hotGoodsList
      } else {
        return []
      }
    }
  },
  async mounted() {
    try {
      // console.log(api);
      // let httpUrl = 'http: //127.0.0.1:8360/api/index/index'
    let res = await axios.get(api.IndexUrl);
    console.log(res.data);
    this.data = res.data.data;
    } catch (error) {
      console.log(error);
    }
  },
};
</script>

<style lang="less" >
#home {
  .van-grid-item {
    overflow: hidden;
  }
  .brandList {
    .van-grid-item {
      .van-grid-item__content {
        padding: 2px;
      }
      position: relative;
      .title {
        position: absolute;
        top: 25px;
        left: 20px;
      }
      .price {
        position: absolute;
        top: 45px;
        left: 20px;
        font-size: 10px;
        color: rgb(170, 167, 167);
      }
    }
  }
  .popularity {
    .van-card__content {
      margin-top: 15px;
      margin-left: -25px;
    }
    position: relative;
    .van-card__title {
      font-weight: 900;
      margin-left: -45px;
    }
    .van-card__desc {
      margin-left: -25px;
    }
    .van-card__price {
      position: absolute;
      top: 38px;
      left: 52px;
      color: red;
    }
  }
  .foot {
    margin-top: 20px;
  }
}
</style>
